#include "wxssearchctrl.h"
#include "wx/srchctrl.h"

namespace {
  wxsRegisterItem<wxsSearchCtrl> Reg( _T( "SearchCtrl" ), _T( "搜索框" ), wxsTWidget, _T( "标准" ), 130 );
  WXS_ST_BEGIN( wxsSearchCtrlStyles, wxEmptyString )
  WXS_ST_CATEGORY( "wxSearchCtrl" )
  WXS_ST( wxTE_PROCESS_ENTER )
  WXS_ST( wxTE_PROCESS_TAB )
  WXS_ST( wxTE_NOHIDESEL )
  WXS_ST( wxTE_LEFT )
  WXS_ST( wxTE_CENTRE )
  WXS_ST( wxTE_RIGHT )
  WXS_ST( wxTE_CAPITALIZE )
  WXS_ST_DEFAULTS()
  WXS_ST_END()
  WXS_EV_BEGIN( wxsSearchCtrlEvents )
  WXS_EVI( EVT_TEXT, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent, Text )
  WXS_EVI( EVT_TEXT_ENTER, wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent, TextEnter )
  WXS_EVI( EVT_SEARCHCTRL_SEARCH_BTN, wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN, wxCommandEvent, SearchClicked )
  WXS_EVI( EVT_SEARCHCTRL_CANCEL_BTN, wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN, wxCommandEvent, CancelClicked )
  WXS_EV_END()
}

wxsSearchCtrl::wxsSearchCtrl( wxsItemResData* Data ):
  wxsWidget( Data, &Reg.Info, wxsSearchCtrlEvents, wxsSearchCtrlStyles ),
  m_sValue( wxEmptyString ),
  m_bShowSearchBtn( true ),
  m_bShowCancelBtn( false ) {}

void wxsSearchCtrl::OnBuildCreatingCode() {
  switch( GetLanguage() ) {
    case wxsCPP: {
      AddHeader( _T( "<wx/srchctrl.h>" ), GetInfo().ClassName, hfInPCH );
      Codef( _T( "%C(%W, %I, %t, %P, %S, %T, %V, %N);\n" ), m_sValue.wx_str() );
      if( !m_bShowSearchBtn ) {
        Codef( _T( "%AShowSearchButton(%b);\n" ), m_bShowSearchBtn );
      }
      if( m_bShowCancelBtn ) {
        Codef( _T( "%AShowCancelButton(%b);\n" ), m_bShowCancelBtn );
      }
      BuildSetupWindowCode();
      return;
    }
    case wxsUnknownLanguage:
    default: {
      wxsCodeMarks::Unknown( _T( "wxsSearchCtrl::OnBuildCreatingCode" ), GetLanguage() );
    }
  }
}

wxObject* wxsSearchCtrl::OnBuildPreview( wxWindow* parent, long flags ) {
  wxSearchCtrl* preview = new wxSearchCtrl( parent, GetId(), m_sValue, Pos( parent ), Size( parent ), Style() );
  if( !m_bShowSearchBtn ) {
    preview->ShowSearchButton( m_bShowSearchBtn );
  }
  if( m_bShowCancelBtn ) {
    preview->ShowCancelButton( m_bShowCancelBtn );
  }
  return SetupWindow( preview, flags );
}

void wxsSearchCtrl::OnEnumWidgetProperties( M_Code_unused long Flags ) {
  WXS_SHORT_STRING( wxsSearchCtrl, m_sValue, _T( "Value" ), _T( "value" ), wxEmptyString, false )
  WXS_BOOL( wxsSearchCtrl, m_bShowSearchBtn, _T( "Search Button" ), _T( "search_button" ), true )
  WXS_BOOL( wxsSearchCtrl, m_bShowCancelBtn, _T( "Cancel Button" ), _T( "cancel_button" ), false )
}

